/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import ControllerInterface.IControllerMainMapping;
import ViewInterface.IViewMain;
import java.awt.Component;
import javax.swing.JOptionPane;

/**
 *
 * @author Fabian
 */
public class ViewMain extends javax.swing.JFrame implements IViewMain {

    /**
     * Creates new form ViewMain
     */
    public IControllerMainMapping myController;

    /**
     * 
     */
    public ViewMain() {
        initComponents();
        GroupSelection.add(jrbtnMap);
        GroupSelection.add(jrbtnMapCrawling);
        GroupSelection.add(jrbtnDataset);
        jrbtnMap.setSelected(true);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        GroupSelection = new javax.swing.ButtonGroup();
        jLabel2 = new javax.swing.JLabel();
        jBtnConectar = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jCBtipoMotorTargetBD = new javax.swing.JComboBox();
        jtxtBDnameTarget = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jtxtHostTarget = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jtxtTargetUser = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jPasswordTarget = new javax.swing.JPasswordField();
        jrbtnMapCrawling = new javax.swing.JRadioButton();
        jrbtnMap = new javax.swing.JRadioButton();
        jPanelMap = new javax.swing.JPanel();
        jPasswordSource = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jCBtipoMotorSourceBD = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtxtHostSource = new javax.swing.JTextField();
        jtxtSourceUser = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jtxtBDsourceName = new javax.swing.JTextField();
        jrbtnDataset = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Mapper");

        jLabel2.setText("Crawling y Mapeo de datos");

        jBtnConectar.setText("Conectar");
        jBtnConectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnConectarActionPerformed(evt);
            }
        });

        jLabel8.setText("Base de datos Destino");

        jLabel9.setText("Seleccione el motor de base de datos");

        jCBtipoMotorTargetBD.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MySql", "SQL server", "PostGreSQL", "Oracle" }));

        jtxtBDnameTarget.setText("3_ontology_movielens");

        jLabel10.setText("Nombre de la base de datos");

        jtxtHostTarget.setText("localhost");

        jLabel11.setText("Ingrese la dirección de la base de datos (host)");

        jtxtTargetUser.setText("root");

        jLabel12.setText("Usuario:");

        jLabel13.setText("Password:");

        jrbtnMapCrawling.setText("Crawling y mapeo de datos");
        jrbtnMapCrawling.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbtnMapCrawlingActionPerformed(evt);
            }
        });

        jrbtnMap.setText("Mapeo de datos");
        jrbtnMap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbtnMapActionPerformed(evt);
            }
        });

        jLabel3.setText("Base de datos Origen");

        jLabel1.setText("Seleccione el motor de base de datos");

        jCBtipoMotorSourceBD.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MySql", "SQL server", "PostGreSQL", "Oracle" }));

        jLabel5.setText("Nombre de la base de datos");

        jLabel4.setText("Ingrese la dirección de la base de datos (host)");

        jtxtHostSource.setText("localhost");

        jtxtSourceUser.setText("root");

        jLabel7.setText("Password:");

        jLabel6.setText("Usuario:");

        jtxtBDsourceName.setText("digg");

        javax.swing.GroupLayout jPanelMapLayout = new javax.swing.GroupLayout(jPanelMap);
        jPanelMap.setLayout(jPanelMapLayout);
        jPanelMapLayout.setHorizontalGroup(
            jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMapLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addGroup(jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jCBtipoMotorSourceBD, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
                    .addGroup(jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jtxtBDsourceName, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtHostSource, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGroup(jPanelMapLayout.createSequentialGroup()
                            .addGroup(jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel7))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jtxtSourceUser)
                                .addComponent(jPasswordSource, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelMapLayout.setVerticalGroup(
            jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelMapLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCBtipoMotorSourceBD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtHostSource, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtBDsourceName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jtxtSourceUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelMapLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jPasswordSource, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jrbtnDataset.setText("Carga archivo plano");
        jrbtnDataset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbtnDatasetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jBtnConectar, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(190, 190, 190))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanelMap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtBDnameTarget, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtxtHostTarget, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCBtipoMotorTargetBD, javax.swing.GroupLayout.PREFERRED_SIZE, 222, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11)
                            .addComponent(jLabel8)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                    .addComponent(jPasswordTarget, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jtxtTargetUser, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jrbtnMapCrawling)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jrbtnMap, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jrbtnDataset))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(189, 189, 189)
                        .addComponent(jLabel2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jrbtnMapCrawling)
                    .addComponent(jrbtnMap)
                    .addComponent(jrbtnDataset))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCBtipoMotorTargetBD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(27, 27, 27)
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtxtHostTarget, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtxtBDnameTarget, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtxtTargetUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel13)
                            .addComponent(jPasswordTarget, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(9, 9, 9)
                        .addComponent(jPanelMap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(42, 42, 42)
                .addComponent(jBtnConectar)
                .addGap(33, 33, 33))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnConectarActionPerformed
 
        boolean s;
        if(jrbtnDataset.isSelected()){
            s = myController.setDataMode();
        }else if(this.jrbtnMapCrawling.isSelected()){
            s = myController.setCrawlerMode();        
        }else{
        s = myController.setInfoDBSource(this.jtxtHostSource.getText(),
                this.jtxtBDsourceName.getText(),
                this.jtxtSourceUser.getText(),
                this.jPasswordSource.getText(),
                this.jCBtipoMotorSourceBD.getSelectedIndex());
        }
        
        boolean t = myController.setInfoDBTarget(this.jtxtHostTarget.getText(),
                this.jtxtBDnameTarget.getText(),
                this.jtxtTargetUser.getText(),
                this.jPasswordTarget.getText(),
                this.jCBtipoMotorTargetBD.getSelectedIndex());
      

        if (s) {
            if (t) {
                JOptionPane.showMessageDialog(null, "Conexión exitosa!",
                        "Conexión Exitosa",
                        JOptionPane.INFORMATION_MESSAGE); //Confirma la correcta conexion con la BD
            } else {
                JOptionPane.showMessageDialog(null,
                        "Ha fallado la conexión con base de datos 'destino'",
                        "Conexión fallida",
                        JOptionPane.INFORMATION_MESSAGE); //indica si falló la conexion con DBTarget            
            }
        } else {
            JOptionPane.showMessageDialog(null,
                    "Ha fallado la conexión con base de datos 'origen'",
                    "Conexión fallida",
                    JOptionPane.INFORMATION_MESSAGE); //indica si falló la conexion con DBSource    
        }
    }//GEN-LAST:event_jBtnConectarActionPerformed

    private void jrbtnMapCrawlingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbtnMapCrawlingActionPerformed
        if (jrbtnMapCrawling.isSelected()) {
            for (Component c : jPanelMap.getComponents()) {
                c.setEnabled(false);
            }
        }
    }//GEN-LAST:event_jrbtnMapCrawlingActionPerformed

    private void jrbtnMapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbtnMapActionPerformed
        if (jrbtnMap.isSelected()) {
            for (Component c : jPanelMap.getComponents()) {
                c.setEnabled(true);
            }
        }
    }//GEN-LAST:event_jrbtnMapActionPerformed

    private void jrbtnDatasetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbtnDatasetActionPerformed
        if (jrbtnDataset.isSelected()) {
            for (Component c : jPanelMap.getComponents()) {
                c.setEnabled(false);
            }
        }
    }//GEN-LAST:event_jrbtnDatasetActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ViewMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ViewMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ViewMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ViewMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ViewMain().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup GroupSelection;
    private javax.swing.JButton jBtnConectar;
    private javax.swing.JComboBox jCBtipoMotorSourceBD;
    private javax.swing.JComboBox jCBtipoMotorTargetBD;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanelMap;
    private javax.swing.JPasswordField jPasswordSource;
    private javax.swing.JPasswordField jPasswordTarget;
    private javax.swing.JRadioButton jrbtnDataset;
    private javax.swing.JRadioButton jrbtnMap;
    private javax.swing.JRadioButton jrbtnMapCrawling;
    private javax.swing.JTextField jtxtBDnameTarget;
    private javax.swing.JTextField jtxtBDsourceName;
    private javax.swing.JTextField jtxtHostSource;
    private javax.swing.JTextField jtxtHostTarget;
    private javax.swing.JTextField jtxtSourceUser;
    private javax.swing.JTextField jtxtTargetUser;
    // End of variables declaration//GEN-END:variables

    /**
     * 
     * @param b
     */
    @Override
    public void setVisibleViewMain(boolean b) {
        this.setVisible(b);
    }

    /**
     * 
     * @param mainController
     */
    @Override
    public void setController(IControllerMainMapping mainController) {
        this.myController = mainController;
    }
}
